<?php

$__FILEPATH__ = dirname(__FILE__)."/";

require_once($__FILEPATH__."./func_common.php");
require_once($__FILEPATH__."./class_def.php");
require_once($__FILEPATH__."./conf/info.php");

$mysqli = GetMysqliInstance();

if(isset($_POST["download"]))
{
	ob_start();
	$fid = intval($_POST["fid"]);
	$sql = sprintf("select src,filename from filelist where id ='%d'", $fid);
	$result = $mysqli->query($sql);
	if ($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$src = $row["src"];
		$filename = $row["filename"];
		$result->close();
		if (file_exists($__FILEPATH__."../admin/".$src))
		{
			ob_end_clean();
			ob_get_clean();
			header("HTTP/1.0 200 OK");
			header("Content-Type: application/octet-stream");
			header("Content-Disposition: attachment; filename=$filename");
			@readfile($__FILEPATH__."../admin/".$src);
			ob_flush();
			$sql = sprintf("update filelist set downcnt = downcnt + 1 where id=%d", $fid);
			$mysqli->query($sql);
			$mysqli->close();
			exit(0);
		}
	}
	ob_end_clean();
	ob_get_clean();
	header("HTTP/1.0 404 Not Found");
	header("Content-Type: text/plain; charset=UTF-8");
	$mysqli->close();
	exit(0);
}


header("Content-Type: text/html; charset=UTF-8");

$PageArray = array();
$result = $mysqli->query("select * from filelist order by time desc", MYSQLI_USE_RESULT);
$PageArray["filelist"] = array();
if ($result)
{
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$FileItem = new FileListItem();
		$FileItem->id 			= $row["id"];
		$FileItem->user 		= $row["user"];
		$FileItem->ip 			= $row["ip"];
		$FileItem->time 		= $row["time"];
		$FileItem->size 		= $row["size"];
		$FileItem->downcnt 		= $row["downcnt"];
		$FileItem->src 			= $row["src"];
		$FileItem->filename 	= $row["filename"];
		$FileItem->intraduce 	= $row["intraduce"];

		$PageArray["filelist"][] = $FileItem;
	}
	$result->close();
}

$mysqli->close();

?>